package com.liangzhong.proxy.footballLottery
{
	import com.adobe.serialization.json.JSON;
	import com.liangzhong.commandconst.guess.CMD_Guess;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.footballLottery.FootballLotteryDataManager;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *竞猜proxy 
	 * @author lake
	 * 
	 */	
	public class FootballLotteryProxy extends BaseProxy
	{
		public static const ProxyName :String = "FootballLotteryProxy";
		/**
		 *竞猜列表返回 
		 */		
		public static const Get_GuessList_Result 	:String = "FootballLotteryProxy/Get_GuessList_Result";
		/**
		 *下注返回 
		 */		
		public static const Get_BET_Result			:String = "FootballLotteryProxy/Get_BET_Result";
		/**
		 * 获取我的竞猜列表
		 */		
		public static const Get_MyGuessList_Result	:String = "FootballLotteryProxy/Get_MyGuessList_Result";
		
		public function FootballLotteryProxy(proxyName:String=ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Logic.MAIN://主
				{
					return onDeal(mainCmdID,subCmdID,buffer,dataSize);
				}
			}
			return false;
		}
		/**
		 * 处理信息
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 */		
		private function onDeal(mainCmdID:uint,
								subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			var retcode :int;
			var type	:int;
			switch(subCmdID)
			{
				case CMD_Guess.BET://下注
				{
					retcode = buffer.readInt();
					if(retcode == 0)//下注成功
					{
						//下注成功
						Alert.showErrorTip("下注成功");
						//请求我的竞猜列表
						getMyGuessList();
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					return true;
				}
			}
			return false;
		}
		/**
		 *获取竞猜列表 
		 * 
		 */		
		public function getGuessList():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.GUESS_GETGUESSLIST;
			ProxyUtils.instance.webClient(getGuessListRes,variables,false);
		}
		/**
		 * 获取竞猜列表  result
		 * @param data
		 */		
		private function getGuessListRes(data :Object):void
		{
			var str:String = String(data);
			var resultObj :Object;
			try
			{
				resultObj = JSON.decode(str)[0];
			}
			catch(e :Error)
			{
				trace("getGuessListRes:",e);
				trace("data:",str);
				return;
			}
			if(resultObj == null)
			{
				trace("",str);
				return;
			}
			if(resultObj.retcode != 0)
			{
				return;
			}
			FootballLotteryDataManager.instance.parseGuessList(resultObj);
			this.sendNotification(Get_GuessList_Result);
		}
		/**
		 *下注 
		 * @param param
		 * choice 1为主队胜，2为两队打平，3为主队负
		 */		
		public function bet(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 13,CMD_Logic.MAIN,CMD_Guess.BET,0);
			sendBuf.writeInt(param.gid);
			sendBuf.writeByte(param.choice);
			sendBuf.writeInt(param.coin);
			sendBuf.writeInt(param.cash);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *获取我的竞猜列表 
		 * 
		 */		
		public function getMyGuessList():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.GUESS_GETMYGUESSLIST;
			ProxyUtils.instance.webClient(getMyGuessListRes,variables,false);
		}
		/**
		 * 获取竞猜列表  result
		 * @param data
		 */		
		private function getMyGuessListRes(data :Object):void
		{
			var str:String = String(data);
			var resultObj :Object;
			try
			{
				resultObj = JSON.decode(str)[0];
			}
			catch(e :Error)
			{
				trace("getMyGuessListRes:",e);
				trace("data:",str);
				return;
			}
			if(resultObj == null)
			{
				trace("",str);
				return;
			}
			if(resultObj.retcode != 0)
			{
				return;
			}
			FootballLotteryDataManager.instance.parseMyGuessList(resultObj);
		}
	}
}